<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>
    <style>
        h1{
            text-align: center;
        }
        .news_body{
            width: 1000px;
            margin: auto;
        }
        .summary{
            color: red;
        }
        .date{
            color: gray;
        }
        img{
            display: block;
            width: fit-content;
            margin: auto;
        }
    </style>
</head>

<body>
    <div class="template">

    </div>
    <script class="entry-template" type="text/x-handlebars-template">
          <h1>{{title}}</h1>
          <div class="news_body">
              <p class="date">发表时间：{{release_date}}</p>
              <p class="summary">摘要{{summary}}</p>
                {{#each content}}
                    {{#compare type}}
                        <p>{{value}}</p>
                        {{else}}
                        <img src="{{value}}" alt="" srcset="">
                    {{/compare}}
                {{/each}}
          </div>
        </div>
    </script>

    <script>
        $(()=>{
            let template=$('.entry-template').html()
            Handlebars.registerHelper('compare', function (v1,option) {
                if(v1=='string'){
                    return option.fn(this)
                }else{
                    return option.inverse(this)
                }
            });
            axios.get('test.json',null)
            .then(res => {
                console.log(res)
                var source=Handlebars.compile(template);
                $('.template').html(source(res.data));
            })
            .catch(err => {
                //console.log(err); 
            })
        })
        
    </script>
</body>

</html>